package org.example.Thread;

import org.example.persistence.model.DataSourceConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MultiThreadMultiDataSourceExample {
    public static void main(String[] args) {
        // 1. 创建数据源配置列表
        List<DataSourceConfig> dataSourceConfigs = createDataSourceConfigs();

        // 2. 使用线程池创建多线程连接多个数据源
        ExecutorService executor = Executors.newFixedThreadPool(dataSourceConfigs.size());

        for (DataSourceConfig config : dataSourceConfigs) {
            executor.execute(() -> {
                // 3. 连接数据库，并执行操作
                Connection connection = connectToDataSource(config);
                executeOperations(connection);
                closeConnection(connection);
            });
        }

        // 4. 等待所有线程执行完成
        executor.shutdown();
        try {
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("所有线程执行完成");
    }

    private static List<DataSourceConfig> createDataSourceConfigs() {
        List<DataSourceConfig> dataSourceConfigs = new ArrayList<>();

        // 添加数据源配置信息
        dataSourceConfigs.add(new DataSourceConfig("jdbc:mysql://localhost:3306/db1", "username1", "password1"));
        dataSourceConfigs.add(new DataSourceConfig("jdbc:mysql://localhost:3306/db2", "username2", "password2"));

        return dataSourceConfigs;
    }

    private static Connection connectToDataSource(DataSourceConfig config) {
        Connection connection = null;

        try {
            // 连接数据库
            connection = DriverManager.getConnection(config.getUrl(), config.getUsername(), config.getPassword());
            System.out.println("连接到数据库：" + config.getUrl());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;
    }

    private static void executeOperations(Connection connection) {
        try {
            // 执行数据库查询操作
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM table_name");

            // 处理查询结果
            while (resultSet.next()) {
                // TODO: 处理查询结果
            }

            resultSet.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void closeConnection(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
                System.out.println("关闭数据库连接");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}